package com.wfh.activity;

import com.wfh.R;
import com.wfh.R.layout;
import com.wfh.R.menu;
import com.wfh.share.OnCompleteListner;
import com.wfh.share.QQShare;
import com.wfh.share.SinaWeiBo;
import com.wfh.util.Util;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class ShareListActivity extends CommonActivity {

	private Button qqbtn, sinabtn;
	private QQShare qqapi;
	private SinaWeiBo sinaapi;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		qqapi = new QQShare(this);
		sinaapi = new SinaWeiBo(this);
		super.onCreate(savedInstanceState, R.layout.activity_sharelist);

		// 组件初始化
		qqbtn = (Button) findViewById(R.id.qqbtn);
		sinabtn = (Button) findViewById(R.id.sinabtn);

		if (Util.getShare(this, "sinatoken") == null
				|| Util.getShare(this, "sinatoken").equals("")) {
			sinabtn.setText("绑定");
		} else {
			sinabtn.setText("注销");
		}

		if (Util.getShare(this, "qqtoken") == null
				|| Util.getShare(this, "qqtoken").equals("")) {
			qqbtn.setText("绑定");
		} else {
			qqbtn.setText("注销");
		}

		// 注册事件

		qqapi.setOnCompleteListener(new OnCompleteListner() {
			@Override
			public void onComplete() {
				runOnUiThread(new Runnable() {
					@Override
					public void run() {
						qqbtn.setText("注销");
					}
				});
			}
		});

		sinaapi.setOnCompleteListener(new OnCompleteListner() {
			@Override
			public void onComplete() {
				runOnUiThread(new Runnable() {
					@Override
					public void run() {
						sinabtn.setText("注销");
					}
				});
			}
		});

		// 点击事件

		qqbtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String flag = qqbtn.getText().toString();
				if (flag.equals("绑定")) {
					qqapi.authorize();
				} else {
					Util.saveShare(ShareListActivity.this, "qqtoken", "");
					qqbtn.setText("绑定");
				}
			}
		});

		// 点击事件

		sinabtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String flag = sinabtn.getText().toString();
				if (flag.equals("绑定")) {
					sinaapi.authorize();
				} else {
					Util.saveShare(ShareListActivity.this, "sinatoken", "");
					sinaapi.loginOut();
					sinabtn.setText("绑定");
				}
			}
		});

	}
	
	@Override
	protected void onDestroy() {
		qqapi.unReceiver();
		super.onDestroy();
	}

}
